package cn.ciis.ciis.dictionary;

import cn.ciis.basic.base.BaseController;
import cn.ciis.basic.model.Dictionary;
import cn.ciis.ciis.equipment.EquipmentAdminService;
import cn.ciis.sys.permission.Notes;
import cn.ciis.sys.permission.Remark;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

/**
 * 数据字典管理控制器
 * 描述：
 * @author 冉志林
 */
public class DictionaryAdminController extends BaseController {

    @Inject
    DictionaryAdminService srv;
    @Inject
    EquipmentAdminService equipmentAdminService;

    @Inject
    DictionaryAdminService dictionaryAdminService;


	@Notes(value = "数据字典管理")
    @Remark(value = "数据字典列表")
    public void index() {
        Dictionary mode = getModel(Dictionary.class, "", true);
        kModel(mode, srv.paginate(getInt(PAGENUMBER, 1), getInt(PAGESIZE, 10), mode));
    }

    @Remark(value = "数据字典添加或编辑页")
    public void addOrEdit(Integer id) {
        set("firstDictionaryList",srv.findFirstDictionaryList());
	    kAddOrEdit(srv.findById(id));
    }

    @Remark(value = "数据字典保存或修改")
    @Before(DictionaryAdminValidator.class)
    public void saveOrUpdate() {
        Dictionary bean = getBean(Dictionary.class);
        if (bean.getPid()!=0) {
            Record rd = srv.findMaxNuber(bean.getPid());
            if (StrKit.isBlank(rd.get("maxNumber"))){
                bean.setNumber(1);
            }else{
                bean.setNumber(rd.getInt("maxNumber")+1);
            }
        }
        bean.setState(0);
        renderJson(srv.saveOrUpdate(bean));
    }

    @Remark(value = "更改字典状态")
    public void modifyState(int id,int state){
        if(dictionaryAdminService.modifyState(id,state)){
            kSuccess("更改字典状态成功");
        }else{
            kFail("更改字典状态失败！");
        }
    }
}